package org.example.manage.service.statistics;

import org.example.framework.domain.statistics.*;
import org.example.framework.model.response.CommonCode;
import org.example.framework.model.response.ObjectResult;
import org.example.manage.service.wordSave.WordSaveService;
import org.example.system.SystemKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: wwy
 * @Date: 2020/11/10 13:52
 */
@Service
public class StatisticsService {
    @Autowired
    private WordSaveService wordSaveService;
    @Value("${hadoop.outputPath}")
    private String path;

    /**
     * 作者书籍数量排序
     * @return
     */
    public ObjectResult authorBookNumSort(){
        AuthorBookNum authorBookNum = new AuthorBookNum();
        List<String> authorNameList = new ArrayList<>();
        List<Integer> bookNumList = new ArrayList<>();
        authorBookNum.setAuthorNameList(authorNameList);
        authorBookNum.setBookNumList(bookNumList);
        List<String> list = wordSaveService.readFile(path + SystemKey.AUTHOR_BOOK_NUM_FILE_NAME+"/part-r-00000", 20);
        for (String s : list) {
            String[] split = s.split("\t");
            authorNameList.add(split[0]);
            bookNumList.add(Integer.parseInt(split[1]));
        }
        return new ObjectResult(CommonCode.SUCCESS,authorBookNum);

    }

    /**
     * 出版社书籍数量排序
     * @return
     */
    public ObjectResult publisherBookNumSort(){
        PublisherBookNum publisherBookNum = new PublisherBookNum();
        List<String> publisherNameList = new ArrayList<>();
        List<Integer> bookNumList = new ArrayList<>();
        publisherBookNum.setPublisherNameList(publisherNameList);
        publisherBookNum.setBookNumList(bookNumList);
        List<String> list = wordSaveService.readFile(path + SystemKey.PUBLISHER_BOOK_NUM_FILE_NAME+"/part-r-00000", 20);
        for (String s : list) {
            String[] split = s.split("\t");
            publisherNameList.add(split[0]);
            bookNumList.add(Integer.parseInt(split[1]));
        }
        return new ObjectResult(CommonCode.SUCCESS,publisherBookNum);
    }

    /**
     * 每年书籍出版数量
     * @return
     */
    public ObjectResult publisherYearBookNum(){
        PublisherYearBookNum publisherYearBookNum = new PublisherYearBookNum();
        List<String> publisherYearList = new ArrayList<>();
        List<Integer> bookNumList = new ArrayList<>();
        publisherYearBookNum.setPublisherYearList(publisherYearList);
        publisherYearBookNum.setBookNumList(bookNumList);
        List<String> list = wordSaveService.readFile(path + SystemKey.PUBLISHER_YEAR_BOOK_NUM_FILE_NAME+"/part-r-00000", null);
        for (String s : list) {
            String[] split = s.split("\t");
            publisherYearList.add(split[0]);
            bookNumList.add(Integer.parseInt(split[1]));
        }
        return new ObjectResult(CommonCode.SUCCESS,publisherYearBookNum);
    }

    /**
     * 作者评分平均数
     * @return
     */
    public ObjectResult authorRatingScoreSortBookNum(){
        AuthorRatingScore authorRatingScore = new AuthorRatingScore();
        List<String> authorRatingScoreList = new ArrayList<>();
        List<Double> ratingScoreAvgList = new ArrayList<>();
        authorRatingScore.setAuthorNameList(authorRatingScoreList);
        authorRatingScore.setRatingScoreAvgList(ratingScoreAvgList);
        List<String> list = wordSaveService.readFile(path + SystemKey.AUTHOR_RATING_SCORE_AVG_FILE_NAME+"/part-r-00000", 20);
        for (String s : list) {
            String[] split = s.split("\t");
            authorRatingScoreList.add(split[0]);
            ratingScoreAvgList.add(Double.parseDouble(split[1]));
        }
        return new ObjectResult(CommonCode.SUCCESS,authorRatingScore);
    }

    /**
     * 作者评分平均数
     * @return
     */
    public ObjectResult priceScope(){
        PriceScope priceScope = new PriceScope();
        List<String> priceScopeNameList = new ArrayList<>();
        List<Double> countNumList = new ArrayList<>();
        priceScope.setPriceScopeNameList(priceScopeNameList);
        priceScope.setCountNumList(countNumList);
        List<String> list = wordSaveService.readFile(path + SystemKey.PRICE_SCOPE_FILE_NAME+"/part-r-00000", 20);
        for (String s : list) {
            String[] split = s.split("\t");
            priceScopeNameList.add(split[0]);
            countNumList.add(Double.parseDouble(split[1]));
        }
        return new ObjectResult(CommonCode.SUCCESS,priceScope);
    }
}
